<script setup>
import { getNoticeApi, deleteNoticeApi, addNoticeApi, changeNoticeApi } from '@/api/notice'
import { ref, onMounted } from 'vue'
import FromDrawer from '@/components/from-drawer.vue'
import { toast } from '@/utils/elnotification'
import ListHeader from '@/components/list-header.vue'

//绑定表格数据
const tableData = ref([])
//总页数
const total = ref(0)
//当前页
const currentPage = ref(1)
//加载动画
const loading = ref(false)
//公告标题
const title1 = ref('')

//获取表格数据
const getData = async (p = null) => {
  if (typeof p === 'number') {
    currentPage.value = p
  }
  loading.value = true
  try {
    const res = await getNoticeApi(currentPage.value)
    tableData.value = res.list
    total.value = res.totalCount
  } catch (error) {
  } finally {
    loading.value = false
  }
}

//删除公告
const onDelete = async (row) => {
  fromDrawerRef.value.showLoading()
  try {
    await deleteNoticeApi(row.id)
    toast('删除成功')
    getData(1)
  } catch (error) {
    toast('删除失败', 'error')
  } finally {
    fromDrawerRef.value.hideLoading()
  }
  console.log(row)
}

//新增公告
const fromDrawerRef = ref(null)
const onAdd = () => {
  title1.value = '新增公告'
  fromDrawerRef.value.open()
  form.value = {
    title: '',
    content: ''
  }
}

//表单部分
const formRef = ref(null)
const form = ref({
  title: '',
  content: ''
})

//表单校验规则
const rules = {
  title: [{ required: true, message: '标题公告不能为空', trigger: 'blur' }],
  content: [{ required: true, message: '标题内容不能为空', trigger: 'blur' }]
}

//确认提交
const onSubmit = async () => {
  const val = await formRef.value.validate((val) => val)
  if (!val) return
  fromDrawerRef.value.showLoading()
  if (changId.value !== 0) {
    try {
      await changeNoticeApi(changId.value, { ...form.value })
      toast('编辑成功')
      getData(1)
    } catch (error) {
      toast('编辑失败', 'error')
    } finally {
      fromDrawerRef.value.hideLoading()
    }
  } else {
    try {
      await addNoticeApi({ ...form.value })
      toast('新增成功')
      getData(1)
    } catch (error) {
      toast('新增失败', 'error')
    } finally {
      fromDrawerRef.value.hideLoading()
    }
  }
}

//修改公告
const changId = ref(0)
const onChange = (row) => {
  title1.value = '修改公告'
  const { id, title, content } = row
  form.value = {
    title,
    content
  }
  changId.value = id
  fromDrawerRef.value.open()
}

onMounted(() => {
  getData()
})
</script>

<template>
  <el-card shadow="never" class="border-0" v-loading="loading">
    <ListHeader @create="onAdd" @refresh="getData"></ListHeader>
  </el-card>
  <el-table :data="tableData" stripe style="width: 100%">
    <el-table-column prop="title" label="公告标题" />
    <el-table-column prop="create_time" label="创建时间" />
    <el-table-column prop="content" label="公告内容" />
    <el-table-column label="操作" width="180" align="center" fixed="right">
      <template #default="scoped">
        <el-button type="primary" size="small" text @click="onChange(scoped.row)">修改</el-button>

        <el-popconfirm
          title="是否要删除该公告？"
          confirm-button-text="确认"
          cancel-button-text="取消"
          @confirm="onDelete(scoped.row)"
        >
          <template #reference>
            <el-button type="primary" size="small" text>删除</el-button>
          </template>
        </el-popconfirm>
      </template>
    </el-table-column>
  </el-table>
  <div class="flex justify-center justify-center mt-5">
    <el-pagination
      @current-change="getData"
      :current-page="currentPage"
      background
      layout="prev,pager,next"
      :total="total"
    />
  </div>
  <FromDrawer ref="fromDrawerRef" :title="title1" @submit="onSubmit">
    <el-form :model="form" ref="formRef" :rules="rules" label-width="80px" :inline="false">
      <el-form-item label="公告标题" prop="title">
        <el-input v-model="form.title" placeholder="请输入标题公告"></el-input>
      </el-form-item>
      <el-form-item label="公告内容" prop="content">
        <el-input
          v-model="form.content"
          placeholder="请输入标题内容"
          type="textarea"
          :rows="5"
        ></el-input>
      </el-form-item>
    </el-form>
  </FromDrawer>
</template>

<style scoped></style>
